<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

class CollectController extends Controller
{
     /**
     * @api {get} /collect GetCollectList
     * @apiVersion 0.2.0
     * @apiName GetCollectList
     * @apiGroup collect
     * @apiPermission app
     *
     * @apiDescription 获取收藏列表
     *
     * @apiParam {Number} user_id *用户id.
     * @apiParam {Number} page *页数.
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Object[]} result 数据.
     * @apiSuccess {Number} result.id 名称.
     * @apiSuccess {String} result.title 名称.
     * @apiSuccess {String} result.description 描述.
     * @apiSuccess {String} result.url 地址.
     * @apiSuccess {String} result.imgurl 图片地址.
     * @apiSuccess {Number} result.user_id 用户id.
     * @apiSuccess {Datetime} result.created_at 创建时间.
     * @apiSuccess {String} message 提示消息
     *
     * @apiError statusError 状态错误.
     * @apiSampleRequest http://dev.timepicker.cn/api/collect
     */
	function index(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'   => 'required|integer|min:1',
                'page'      => 'integer|min:1'
            ]
        );
        if($validator->fails())
            return $this::jsonResponse(true,$validator->messages(),"paramError");

        $userId = $input["user_id"];
        $skip   = empty($input["page"])?0:20*($input["page"]-1);

        $starResult = DB::table("cl_collect")
          ->select("id","title","description","url","imgurl","created_at"
               )
          ->where("user_id",$userId)
          ->orderBy("created_at","DESC")
          ->skip($skip)->take(20)
          ->get();

		return $this::jsonResponse(false,$starResult);
	}

    /**
     * @api {post} /collect CreateCollect
     * @apiVersion 0.2.0
     * @apiName CreateCollect
     * @apiGroup collect
     * @apiPermission app
     *
     * @apiDescription 创建收藏
     *
     * @apiParam {String} title *名称.
     * @apiParam {String} description 描述.
     * @apiParam {String} url *地址.
     * @apiParam {String} imgurl 图片地址.
     * @apiParam {Number} user_id *用户id.
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Number} result 新建收藏id.
     * @apiSuccess {String} message 提示消息.
     *
     * @apiError paramError 参数错误.
     * @apiSampleRequest http://192.168.1.120:82/api/collect
     */
     public function store(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'title'       => 'required|string|min:1',
                'description' => 'string|min:2',
                'imgurl'      => 'url|min:1',
                'url'         => 'required|url|min:1',
                'user_id'     => 'required|integer|min:1',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }
        
        $collectData = [
            "title"   => $input["title"],
            "description" => empty($input["description"])?"":$input["description"],
            "imgurl"  => empty($input["imgurl"])?"":$input["imgurl"],
            "url"     => $input["url"],
            "user_id" => $input["user_id"]
        ];

        $collectId = DB::table("cl_collect")->insertGetId($collectData);

        return $this->jsonResponse(false,$collectId);
     }

    /**
     * @api {delete} /collect/{id} DeleteCollect
     * @apiVersion 0.2.0
     * @apiName DeleteCollect
     * @apiGroup collect
     * @apiPermission app
     *
     * @apiDescription 删除收藏
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Boolean} result 结果.
     * @apiSuccess {String} message 提示消息.
     *
     * @apiError paramError 参数错误.
     * @apiSampleRequest http://192.168.1.120:82/api/collect/1
     */
     public function destroy($collectId){

        $collectResult = DB::table("cl_collect")
            ->where("id",$collectId)
            ->delete();

        return $this->jsonResponse(false,$collectResult);
     }

}